package com.qf.java2106.huangshengrui.hotel.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.qf.java2106.huangshengrui.hotel.common.ResultVO;
import com.qf.java2106.huangshengrui.hotel.dao.IFoodTypeDao;
import com.qf.java2106.huangshengrui.hotel.factory.BeanFactory;
import com.qf.java2106.huangshengrui.hotel.pojos.Food;
import com.qf.java2106.huangshengrui.hotel.pojos.FoodType;
import com.qf.java2106.huangshengrui.hotel.service.IFoodTypeService;

import java.sql.SQLException;
import java.util.List;

import static com.qf.java2106.huangshengrui.hotel.constant.MessageConstant.*;

/**
 * @author q
 * @version V1.0
 * @date 2021/10/25
 **/
public class FoodTypeServiceImpl implements IFoodTypeService {

    IFoodTypeDao foodTypeDao = (IFoodTypeDao) BeanFactory.getBean("foodTypeDao");

    @Override
    public ResultVO findAllType() {
        try {
            return ResultVO.ok(FOODTYPE_QUERY_SUCCESS,foodTypeDao.findAllType());
        } catch (SQLException e) {
            e.printStackTrace();
            return ResultVO.error(FOODTYPE_QUERY_FAIL,e.getMessage());
        }
    }

    @Override
    public ResultVO findTypeLikeKey(String key) {
        try {
            if (StringUtils.isEmpty(key)){
                key="%%";
            } else {
                key="%"+ key.trim() + "%";
            }
            return ResultVO.ok(FOODTYPE_QUERY_SUCCESS,foodTypeDao.findTypeLikeKey(key));
        } catch (SQLException e) {
            e.printStackTrace();
            return ResultVO.error(FOODTYPE_QUERY_FAIL,e.getMessage());
        }
    }

    @Override
    public ResultVO saveType(FoodType foodType) {
        try {
            return ResultVO.ok(FOODTYPE_SAVE_SUCCESS,foodTypeDao.saveType(foodType));
        } catch (SQLException e) {
            e.printStackTrace();
            return ResultVO.error(FOODTYPE_SAVE_FAIL,e.getMessage());
        }
    }

    @Override
    public ResultVO findTypeById(Integer id) {
        try {
            return ResultVO.ok(FOODTYPE_QUERY_SUCCESS,foodTypeDao.findTypeById(id));
        } catch (SQLException e) {
            e.printStackTrace();
            return ResultVO.error(FOODTYPE_QUERY_FAIL,e.getMessage());
        }
    }

    @Override
    public ResultVO updateType(FoodType foodType) {
        try {
            return ResultVO.ok(FOODTYPE_UPDATE_SUCCESS,foodTypeDao.updateType(foodType));
        } catch (SQLException e) {
            e.printStackTrace();
            return ResultVO.error(FOODTYPE_UPDATE_FAIL,e.getMessage());
        }
    }

    @Override
    public ResultVO deleteType(Integer id) {
        try {
            return ResultVO.ok(FOODTYPE_DELETE_SUCCESS,foodTypeDao.deleteType(id));
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVO.error(FOODTYPE_DELETE_FAIL,e.getMessage());
        }
    }

    @Override
    public ResultVO findTypeByName(String typeName) {
        try {
            if (StringUtils.isEmpty(typeName)){
                return ResultVO.error(FOODTYPENAME_IS_NOT_NULL,FOODTYPENAME_IS_NOT_NULL);
            }
            if (null!=foodTypeDao.findTypeByName(typeName)){
                return ResultVO.error(FOODTYPENAME_IS_EXISTS);
            } else {
                return ResultVO.ok(FOODTYPENAME_IS_NOT_EXISTS);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVO.error(e.getMessage());
        }
    }

    @Override
    public ResultVO checkDeleteType(Integer id) {
        try {
            List<Food> foods = foodTypeDao.checkDeleteType(id);
            if (0 != foods.size()){
                return ResultVO.error(FOODTYPE_HAS_FOOD,foods);
            } else {
                return ResultVO.ok(FOODTYPE_NOT_FOOD);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return ResultVO.error(e.getMessage());
        }
    }
}
